# -*- coding: utf8 -*-


import time
import json
import pymongo
import config

from scrapy import Spider
from scrapy.http import Request
from sqlhelper import SqlHelper


class CoinMarket(Spider):
    name = 'mongo_coin_market'

    handle_httpstatus_list = [503, 429]

    # 设置单独使用的请求时间间隔.
    custom_settings = {
        'DOWNLOAD_DELAY': 0.5
    }

    def __init__(self, name = None, **kwargs):
        super(CoinMarket, self).__init__(name, **kwargs)

        self.sql = SqlHelper(db_name = 'bt_news', db_config = config.bt_server)
        command = 'SELECT name FROM bt_exchange;'
        self.exchange_name = self.sql.query(command, cursor_type = 'dict')

        # 连接mongodb
        client = pymongo.MongoClient('47.254.26.182', 27017)
        # client = pymongo.MongoClient('localhost', 27017)
        self.db = client.bt_news

    def start_requests(self):
        for name in self.exchange_name:
            # 交易所名称
            exchange_name = name.get('name')

            # url = 'https://block.cc/api/v1/exchange/tickers?name=cryptopia&page=1&size=50'
            url = 'https://block.cc/api/v1/exchange/tickers?name=%s&page=0&size=50' % exchange_name

            yield Request(
                url=url,
                dont_filter=True,
                headers={
                    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
                },
                meta={
                    'exchange_name': exchange_name,
                },
                callback=self.coin_page
            )

    # 获取交易对页数
    def coin_page(self, response):
        body = json.loads(response.body)

        data = body.get('data')

        # 交易所名称
        exchange_name = response.meta.get('exchange_name')

        # 总页数
        page_count = data.get('pageCount')

        for page in range(0, page_count):
            url = 'https://block.cc/api/v1/exchange/tickers?name=%s&page=%d&size=50' % (exchange_name, page)

            yield Request(
                url=url,
                dont_filter=True,
                headers={
                    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
                },
                meta={
                    'exchange_name': exchange_name,
                },
                callback=self.coin_info
            )

    def coin_info(self, response):
        body = json.loads(response.body)

        # 交易所名称
        exchange_name = response.meta.get('exchange_name')

        data = body.get('data')
        tickers = data.get('tickers')

        if tickers:
            for ticker in tickers:
                # 交易所ID
                exchange_id = ticker.get('_id')

                # 交易对名称
                pair = ticker.get('display_pair_name').split('/')
                pair_name = pair[0]
                variual_pair = pair[1]

                # 流通市值
                market_value = ticker.get('price')

                # 买价
                buy = ticker.get('native_price')

                # 卖价
                sell = ticker.get('price')

                # 最高价
                high = ticker.get('high1d')

                # 最低价
                low = ticker.get('low1d')

                # 最新成交价
                last = ticker.get('price')

                # 成交量（最近的24小时）
                vol = ticker.get('volume')

                # 时间戳
                time_time = ticker.get('timestamps')

                # 交易占比
                roportion = ticker.get('percent')

                # 涨幅
                change1d = ticker.get('change1d')

                kline = ticker.get('enableKline')
                if kline == False:
                    hasklin = 0
                else:
                    hasklin = 1

                # 入库
                data = {
                    'exchange_name': exchange_name,
                    'variual_coin_name': pair_name,
                    'variual_pair': variual_pair,
                    'exchange_id': exchange_id,
                    'market_value': market_value,
                    'buy': buy,
                    'sell': sell,
                    'high': high,
                    'low': low,
                    'last': last,
                    'vol': vol,
                    'time': time_time,
                    'hasklin': hasklin,
                    'roportion': roportion,
                    'change1d': change1d,
                    'updatetime': time.localtime()
                }
                self.db.bt_coin_market.update_one({'exchange_name': exchange_name, 'variual_coin_name':pair_name}, {'$set': data}, upsert=True)

    # close
    def close(spider, reason):
        spider.sql.commit()

# 数据库结构
'''
CREATE TABLE `bt_coin_market` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `exchange_name` varchar(256) DEFAULT NULL COMMENT '交易所名称',
  `exchange_id` varchar(32) DEFAULT NULL,
  `variual_coin_name` varchar(256) DEFAULT NULL COMMENT '货币名称（交易对）',
  `market_value` double DEFAULT NULL COMMENT '流通市值',
  `buy` double DEFAULT NULL COMMENT '买价',
  `sell` double DEFAULT NULL COMMENT '卖价',
  `high` double DEFAULT NULL COMMENT '最高价',
  `low` double DEFAULT NULL COMMENT '最低价',
  `last` double DEFAULT NULL COMMENT '最新成交价',
  `vol` double DEFAULT NULL COMMENT '成交量（最近的24小时）',
  `time` varchar(32) DEFAULT NULL,
  `roportion` double DEFAULT NULL COMMENT '交易占比',
  `updatetime` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7003 DEFAULT CHARSET=utf8 COMMENT='交易行情';
'''